package ui;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;

public class ShowImage extends Shell {

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			ShowImage shell = new ShowImage(display);
			shell.open();
			shell.draw("http://image.sinajs.cn/newchart/daily/n/sh601006.gif");
			shell.draw("http://image.sinajs.cn/newchart/daily/n/sz300215.gif");
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	GC gc = new GC(this);

	public void draw(String urlstr){
		URL url;
		try {
//			redraw();
			url = new URL(urlstr);
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
			DataInputStream in = new DataInputStream(connection.getInputStream());
			Image img = new Image(Display.getDefault(), in);  
			canvas.setData(img);
			canvas.redraw();
			setVisible(true);
//			gc.drawImage(img, 0, 0); 
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			
		}
	}
	
	/**
	 * Create the shell.
	 * @param display
	 */
	Canvas canvas = new Canvas(this, SWT.NONE);
	public ShowImage(Display display) {
		super(display, SWT.NO_TRIM);
		addShellListener(new ShellAdapter() {
			@Override
			public void shellClosed(ShellEvent e) {
				setVisible(false);
				e.doit=false;
			}
		});
		addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				setVisible(false);
			}
		});
		
		canvas.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		canvas.addPaintListener(new PaintListener() {
			public void paintControl(final PaintEvent event) {
				Image image = (Image) canvas.getData();
				if (image != null) {
					event.gc.drawImage(image, 0, 0);// 定位图像左上角距canvas左上角的距离
				}
			}
		});
		canvas.setBounds(-10, 0, 554, 310);
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("日K线");
		setSize(556, 312);
		setBackground(SWTResourceManager.getColor(255, 255, 255));
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
